package com.cup.bddm.impala.controller;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cup.bddm.handler.Token;
import com.cup.bddm.impala.Constant;
import com.cup.bddm.impala.po.User;
import com.cup.bddm.impala.service.UserService;
import com.cup.bddm.impala.vo.UserVo;

@Controller("srv-UserController")
@RequestMapping(value = "/services/user")
public class UserController {

	static Logger logger = Logger.getLogger(UserController.class);

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/login/{account}/{insIdCd}/{pwd}", method = RequestMethod.GET)
	@ResponseBody
	public UserVo getUserByIns(@PathVariable("account") String account, @PathVariable("insIdCd") String insIdCd, @PathVariable("pwd") String pwd) {
		logger.info("login request:loginName:" + account + ",insIdCd:" + insIdCd + ",pwd:" + pwd);
		if (StringUtils.isEmpty(account) || StringUtils.isEmpty(insIdCd) || StringUtils.isEmpty(pwd)) {
			UserVo user = new UserVo();
			user.setState(Constant.PARA_ERROR);
			return user;
		}
		return userService.getUserByIns(account, insIdCd, pwd);
	}

	@Token
	@RequestMapping(value = "/pwd/{account}/{insIdCd}/{pwd}/{newPwd}", method = RequestMethod.GET)
	@ResponseBody
	public UserVo changePwd(@PathVariable("account") String account, @PathVariable("insIdCd") String insIdCd, @PathVariable("pwd") String pwd, @PathVariable("newPwd") String newPwd) {
		logger.info("login request:loginName:" + account + ",insIdCd:" + insIdCd + ",pwd:" + pwd + ",newPwd:" + newPwd);
		if (StringUtils.isEmpty(account) || StringUtils.isEmpty(account) || StringUtils.isEmpty(pwd) || StringUtils.isEmpty(newPwd)) {
			UserVo user = new UserVo();
			user.setState(Constant.PARA_ERROR);
			return user;
		}
		return userService.changePwd(account, insIdCd, pwd, newPwd);
	}

	@Token
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public UserVo addUserInfo(@RequestBody User user) {
		logger.info("insert user info");
		UserVo result = new UserVo();
		result = userService.addUserInfo(user);
		return result;
	}

}
